#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === AOSP 8960 Oreo Automatic Releaser ===
function autoreleaseaospsony8960o()
{
  # Usage: autoreleaseaospsony8960o [devices] (Automated AOSP Oreo Sony 8960 releaser)

  # AndroidDev
  cdand;

  # When option enabled, sync repository tree
  if ! shtools-builds-nosync-active && [ -z "${*}" ]; then
    cdaosp;
    reposy;
  fi;

  # Devices to build
  local devices=${*:-huashan mint hayabusa tsubasa};

  # Access device repository and when option enabled, sync tree
  if ! pathchanged cdaosp 8960 && ! shtools-builds-nosync-active; then
    reposy;
  fi;

  # ROM patcher
  if ! aospsony8960omr1patcher 'a'; then
    return;
  fi;

  # Walk through devices
  for device in ${devices}; do

    # ROM build and release
    romautorelease "${device}" aosp;

  done;

  # When option enabled and not local, clean out folder
  if shtools-builds-clean-active && ! shtools-builds-local-active; then
    repoclean;
  fi;
}

# === LineageOS Automatic Releaser ===
function autoreleaselineagesony8960()
{
  # Usage: autoreleaselineagesony8960 [devices] (Automated LineageOS Sony 8960 releaser)

  # AndroidDev
  cdand;

  # When option enabled, sync repository tree
  if ! shtools-builds-nosync-active && [ -z "${1}" ]; then
    cdlineage;
    reposy;
  fi;

  # Devices to build
  local devices=${*:-huashan mint hayabusa tsubasa};

  # Access device repository and when option enabled, sync tree
  if ! pathchanged cdlineage 8960 && ! shtools-builds-nosync-active; then
    reposy;
  fi;

  # Walk through devices
  for device in ${devices}; do

    # ROM build and release
    romautorelease "${device}" lineage;

  done;

  # When option enabled and not local, clean out folder
  if shtools-builds-clean-active && ! shtools-builds-local-active; then
    repoclean;
  fi;
}

# === LineageOS Automatic Releaser ===
function autoreleaselineagesony8960o()
{
  # Usage: autoreleaselineagesony8960o [devices] (Automated LineageOS 15.0 Sony 8960 releaser)

  # AndroidDev
  cdand;

  # When option enabled, sync repository tree
  if ! shtools-builds-nosync-active && [ -z "${1}" ]; then
    cdlineage;
    reposy;
  fi;

  # Access device repository and when option enabled, sync tree
  if ! pathchanged cdlineage 8960 && ! shtools-builds-nosync-active; then
    reposy;
  fi;

  # Devices to build
  local devices=${*:-huashan mint};

  # Import pending reviews
  lineagesony8960oreopatcher;

  # Apply Sony Huashan and Blue changes
  if ! aospsony8960opatcher 'device/sony/*'; then
    return;
  fi;

  # Walk through devices
  for device in ${devices}; do

    # ROM build and release
    romautorelease "${device}" next;

  done;

  # When option enabled and not local, clean out folder
  if shtools-builds-clean-active && ! shtools-builds-local-active; then
    repoclean;
  fi;
}

# === ResurrectionRemix Sony 8960 Automatic Releaser ===
function autoreleaserrsony8960()
{
  # Usage: autoreleaserrsony8960 [devices] (Automated RR Sony 8960 releaser)

  # AndroidDev
  cdand;

  # When option enabled, sync tree
  if ! shtools-builds-nosync-active; then
    cdlineage;
    reposy;
    cdrr;
    reposy;
  fi;

  # Devices to build
  local devices=${*:-huashan mint hayabusa tsubasa nicki};

  # Walk through devices
  for device in ${devices}; do

    # ROM build and release
    romautorelease "${device}" rr;

  done;

  # When option enabled and not local, clean out folder
  if shtools-builds-clean-active && ! shtools-builds-local-active; then
    repoclean;
  fi;
}

# === ROM AOSP-CAF Sony 8960 Releaser ===
function autoreleasecafsony8960()
{
  # Usage: autoreleasecafsony8960 [devices] (Automated AOSP-CAF Sony 8960 releaser)

  # AndroidDev
  cdand;

  # Access project repository
  cdaospcaf;

  # When option enabled, sync tree
  if ! shtools-builds-nosync-active; then
    reposycl;
  fi;

  # ROM patcher
  if ! aospcafpatcher 'a'; then
    return;
  fi;

  # Devices to build
  local devices=${*:-huashan mint hayabusa tsubasa};

  # Walk through devices
  for device in ${devices}; do

    # ROM build and release
    romautorelease "${device}" caf;

  done;

  # When option enabled and not local, clean out folder
  if shtools-builds-clean-active && ! shtools-builds-local-active; then
    repoclean;
  fi;
}

# === AOSP 8960 Master Automatic Releaser ===
function autoreleaseaospsony8960master()
{
  # Usage
  if [ -z "${*}" ]; then
    echo '';
    echo ' Usage: autoreleaseaospsony8960master <devices> (Automated AOSP Master Sony 8960 releaser)';
    echo '';
    return;
  fi;

  # Launch internal releaser
  __autoreleaseaospsony8960dev 'master' "${@}";
}

# === AOSP 8960 Development Automatic Releaser ===
function __autoreleaseaospsony8960dev()
{
  # Usage
  if [ -z "${1}" ] || [ -z "${*:2}" ]; then
    echo '';
    echo ' Usage: __autoreleaseaospsony8960dev <branch_tag> <devices> (Automated AOSP Development Sony 8960 releaser)';
    echo '';
    return;
  fi;

  # Variables
  local branch_tag=${1};
  local devices=${*:2};

  # AndroidDev
  cdand;

  # When option enabled, sync repository tree
  if ! shtools-builds-nosync-active && [ -z "${devices}" ]; then
    cdaosp;
    reposy;
  fi;

  # Access device repository and when option enabled, sync tree
  if ! pathchanged cdaosp "${branch_tag}" && ! shtools-builds-nosync-active; then
    reposy;
  fi;

  # ROM patcher
  if ! "aospsony8960${branch_tag}patcher" 'a'; then
    return;
  fi;

  # Walk through devices
  for device in ${devices}; do

    # ROM build and release
    romautorelease "${device}" master;

  done;

  # When option enabled, not local and not bringup, clean device out folder
  if shtools-builds-clean-active && ! shtools-builds-local-active && ! shtools-builds-bringup-active; then
    repoclean;
  fi;
}

# === LineageOS 8960 Master Automatic Releaser ===
function autoreleaselineagesony8960master()
{
  # Usage
  if [ -z "${*}" ]; then
    echo '';
    echo ' Usage: autoreleaselineagesony8960master <devices> (Automated LineageOS Sony 8960 Master releaser)';
    echo '';
    return;
  fi;

  # Variables
  local devices=${*};

  # AndroidDev
  cdand;

  # Access device repository and when option enabled, sync tree
  if ! pathchanged cdaosp 'master' && ! shtools-builds-nosync-active; then
    cd .repo/local_manifests/;
    git submodule update --remote;
    cd ../../;
    reposy;
  fi;

  # ROM patcher
  if ! lineagesony8960masterpatcher 'a'; then
    return;
  fi;

  # Walk through devices
  for device in ${devices}; do

    # ROM build and release
    romautorelease "${device}" next;

  done;

  # When option enabled and not local, clean out folder
  if shtools-builds-clean-active && ! shtools-builds-local-active; then
    repoclean;
  fi;
}
